home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 May / EnigmA AMIGA RUN 07 (1996)(G.R. Edizioni)(IT)[!][issue 1996-05][EARSAN CD VI].iso / progs / utilmisc / frontpub / frontpub_c / frontpub.c < prev    next >
C/C++ Source or Header  |  1995-03-27  |  10KB  |  312 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* includes                                                                   */
  4. /*                                                                            */
  5. /******************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/interrupts.h>
  9. #include <hardware/intbits.h>
  10. #include <libraries/commodities.h>
  11. #include <intuition/screens.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <proto/commodities.h>
  14. #include <proto/intuition.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <string.h>
  18.  
  19. /******************************************************************************/
  20. /*                                                                            */
  21. /* compiler specifics                                                         */
  22. /*                                                                            */
  23. /******************************************************************************/
  24.  
  25. #ifdef __SASC
  26.    #define main _main
  27.    #define exit _exit
  28.    #define WbMsg _WBenchMsg
  29.    #define SETA4()
  30.    #define RESA4()
  31. #endif
  32.  
  33. #ifdef __GNUC__
  34.    #define __saveds
  35.    #define main _main
  36.    #define exit _exit
  37.    #define WbMsg _WBenchMsg
  38.    #define SETA4() asm volatile ("\tmovel a4,sp@-\n\tmovel a1,a4\n")
  39.    #define RESA4() asm volatile ("\tmovel sp@+,a4\n")
  40. #endif
  41.  
  42. /******************************************************************************/
  43. /*                                                                            */
  44. /* prototypes                                                                 */
  45. /*                                                                            */
  46. /******************************************************************************/
  47.  
  48. int  InstallCx();
  49. void ParseArgs();
  50. int  OpenLibs();
  51. void CheckFrontScr();
  52. int  CheckBroker();
  53. void CloseLibs();
  54. void RemoveCx();
  55. int  ScreenHandler();
  56.  
  57. /******************************************************************************/
  58. /*                                                                            */
  59. /* useful defines                                                             */
  60. /*                                                                            */
  61. /******************************************************************************/
  62.  
  63. #define OSVERSION 37L
  64. #define NEEDV37 "This program requires at least v37!\n"
  65. #define TEMPLATE "CX_POPUP/K,CX_POPKEY/K,CX_PRIORITY/N/K"
  66.  
  67. /******************************************************************************/
  68. /*                                                                            */
  69. /* symbol imports                                                             */
  70. /*                                                                            */
  71. /******************************************************************************/
  72.  
  73. extern struct WBStartup *WbMsg;
  74. extern struct ExecBase *SysBase;
  75.  
  76. /******************************************************************************/
  77. /*                                                                            */
  78. /* global variables                                                           */
  79. /*                                                                            */
  80. /******************************************************************************/
  81.  
  82. struct DosLibrary *DOSBase;
  83. struct Library *CxBase,*IconBase;
  84. struct IntuitionBase *IntuitionBase;
  85.  
  86. struct Screen *FrontScreen;
  87.  
  88. const UBYTE verstag[]="$VER: FrontPub 1.1 (25.3.95) © 1995 by G.Nikl";
  89.  
  90. /*** commodities stuff ***/
  91.  
  92. struct NewBroker NewBroker =
  93. { NB_VERSION,
  94.   "FrontPub",
  95.   (UBYTE *)&verstag[6],
  96.   "frontmost pubscreen becomes default",
  97.   NBU_UNIQUE,
  98.   0,0,NULL,0
  99. };
  100.  
  101. CxObj *Broker;
  102. LONG CxEnable;
  103.  
  104. /*** interrupt stuff ***/
  105.  
  106. struct Interrupt ScreenIntr =
  107. { 0,0,
  108.   NT_INTERRUPT,
  109.   -1,
  110.   "FrontPub",
  111.   0,
  112.   (VOID (*)())&ScreenHandler
  113. };
  114.  
  115. ULONG IntrSignal,CheckIt=TRUE;
  116.  
  117. /******************************************************************************/
  118. /*                                                                            */
  119. /* implementation                                                             */
  120. /*                                                                            */
  121. /******************************************************************************/
  122.  
  123. /******************************************************************************/
  124. /*                                                                            */
  125. /* install program as commodity                                               */
  126. /*                                                                            */
  127. /******************************************************************************/
  128.  
  129. int InstallCx()
  130. {
  131.   int ret=0;
  132.  
  133.   if ((NewBroker.nb_Port=CreateMsgPort()) != NULL)
  134.   {
  135.     if ((Broker=CxBroker(&NewBroker,NULL)) != NULL)
  136.     {
  137.       CxEnable=TRUE; (void)ActivateCxObj(Broker,CxEnable); ret=1;
  138.     }
  139.   }
  140.   return ret;
  141. }
  142.  
  143. /******************************************************************************/
  144. /*                                                                            */
  145. /* open all libraries                                                         */
  146. /*                                                                            */
  147. /******************************************************************************/
  148.  
  149. int OpenLibs()
  150. {
  151.   int ret=1;
  152.  
  153.   if (((IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library", OSVERSION)) == NULL) ||
  154.       ((CxBase=OpenLibrary("commodities.library", OSVERSION)) == NULL))
  155.   {
  156.     if (!WbMsg && (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library", 0)) != NULL)
  157.     {
  158.       Write(Output(),NEEDV37,sizeof(NEEDV37)); CloseLibrary((struct Library *)DOSBase);
  159.     }
  160.     ret=0;
  161.   }
  162.   return ret;
  163. }
  164.  
  165. /******************************************************************************/
  166. /*                                                                            */
  167. /* main loop                                                                  */
  168. /*                                                                            */
  169. /******************************************************************************/
  170.  
  171. int main()
  172. {
  173. #ifdef __GNUC__
  174.   register void *a4 asm("a4");
  175. #endif
  176.  
  177.   if (OpenLibs())
  178.   {
  179.     if (!((LONG)(IntrSignal=AllocSignal(-1)) < 0))
  180.     {
  181.       if (InstallCx())
  182.       {
  183.         (void)SetPubScreenModes(SetPubScreenModes(0)|SHANGHAI);
  184. #ifdef __GNUC__
  185.         ScreenIntr.is_Data=a4;
  186. #endif
  187.         AddIntServer(INTB_VERTB,&ScreenIntr);
  188.         for(;;)
  189.          {
  190.            ULONG signals=Wait(1L<<IntrSignal|1L<<NewBroker.nb_Port->mp_SigBit|SIGBREAKF_CTRL_C);
  191.  
  192.            if (signals & SIGBREAKF_CTRL_C)
  193.              break;
  194.  
  195.            if (signals & 1L<<IntrSignal)
  196.              CheckFrontScr();
  197.  
  198.            if (CheckBroker())
  199.              break;
  200.          }
  201.         RemIntServer(INTB_VERTB,&ScreenIntr);
  202.       }
  203.     }
  204.   }
  205.   RemoveCx();
  206.   CloseLibs();
  207. }
  208.  
  209. int CheckBroker()
  210. {
  211.   CxMsg *cxmsg;
  212.   int quit=0;
  213.  
  214.   while ((cxmsg=(CxMsg *)GetMsg(NewBroker.nb_Port)) != NULL)
  215.   {
  216.     if (CxMsgType(cxmsg) == CXM_COMMAND)
  217.       switch(CxMsgID(cxmsg))
  218.       {
  219.         case CXCMD_DISABLE:
  220.           CxEnable=FALSE;
  221.           (void)ActivateCxObj(Broker,CxEnable);
  222.           break;
  223.         case CXCMD_ENABLE:
  224.           CxEnable=TRUE;
  225.           (void)ActivateCxObj(Broker,CxEnable);
  226.           break;
  227.         case CXCMD_KILL:
  228.           quit=1;
  229.           break;
  230.       }
  231.     ReplyMsg((struct Message *)cxmsg);
  232.   }
  233.   return quit;
  234. }
  235.  
  236. void CheckFrontScr()
  237. {
  238.   struct PubScreenNode *pubscrnode,*defpub;
  239.   UBYTE pubscrname[MAXPUBSCREENNAME+3];
  240.  
  241.   Forbid(); UnlockIBase(LockIBase(0));
  242.   pubscrnode=(struct PubScreenNode *)LockPubScreenList();
  243.   GetDefaultPubScreen(&pubscrname[0]);
  244.   if ((defpub=(struct PubScreenNode *)FindName((struct List *)pubscrnode,&pubscrname[0])) != NULL)
  245.     if (defpub->psn_Screen != (FrontScreen=IntuitionBase->FirstScreen))
  246.       while ((pubscrnode=(struct PubScreenNode *)pubscrnode->psn_Node.ln_Succ)->psn_Node.ln_Succ != NULL)
  247.         if (pubscrnode->psn_Screen == FrontScreen && !(pubscrnode->psn_Flags&PSNF_PRIVATE))
  248.         {
  249.           strcpy(&pubscrname[0],pubscrnode->psn_Node.ln_Name);
  250.           pubscrnode=NULL;
  251.           break;
  252.         }
  253.   UnlockPubScreenList();
  254.   if (!pubscrnode)
  255.     SetDefaultPubScreen(&pubscrname[0]);
  256.   Permit();
  257.   CheckIt=TRUE;
  258. }
  259.  
  260. /******************************************************************************/
  261. /*                                                                            */
  262. /* close all opened libraries                                                 */
  263. /*                                                                            */
  264. /******************************************************************************/
  265.  
  266. void CloseLibs()
  267. {
  268.   if (CxBase)
  269.     CloseLibrary(CxBase);
  270.   if (IntuitionBase)
  271.     CloseLibrary((struct Library *)IntuitionBase);
  272. }
  273.  
  274. /******************************************************************************/
  275. /*                                                                            */
  276. /* closedown commodity broker                                                 */
  277. /*                                                                            */
  278. /******************************************************************************/
  279.  
  280. void RemoveCx()
  281. {
  282.   if (Broker)
  283.   {
  284.     (void)ActivateCxObj(Broker,FALSE);
  285.     DeleteCxObjAll(Broker);
  286.   }
  287.   if (NewBroker.nb_Port != NULL)
  288.   {
  289.     DeleteMsgPort(NewBroker.nb_Port);
  290.   }
  291. }
  292.  
  293. /******************************************************************************/
  294. /*                                                                            */
  295. /* vertical blank handler                                                     */
  296. /*                                                                            */
  297. /******************************************************************************/
  298.  
  299. int __saveds ScreenHandler()
  300. {
  301.   struct Screen *scr;
  302.  
  303.   SETA4();
  304.   if (CxEnable != FALSE && CheckIt != FALSE)
  305.     if ((scr=IntuitionBase->FirstScreen) != NULL && scr != FrontScreen)
  306.     {
  307.       Signal(NewBroker.nb_Port->mp_SigTask,1L<<IntrSignal); CheckIt=FALSE;
  308.     }
  309.   RESA4();
  310.   return 0;
  311. }
  312.